<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="/shop/include :: header"></head>
<body>
<form class="layui-form" action="" lay-filter="shop_supply_info-add-form" style="margin-top: 20px;">
    <div style="display: flex">
        <div class="layui-form-item" style="display: contents">
            <label class="layui-form-label">头像：</label>
            <div class="layui-upload-list" id="test1">
                <img class="layui-upload-img" id="demo1" style="width: 190px;height: 190px">
                <input type="hidden" name="supplyHead" id="supplyHead">
            </div>
        </div>
        <div style="width: 73%">
            <div class="layui-form-item">
                <label class="layui-form-label">名称：</label>
                <div class="layui-input-block">
                    <input id="supplyName" name="supplyName" placeholder="请输入名称"
                           class="layui-input" lay-verify="required" autocomplete="off">
                </div>
            </div>
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">负责人：</label>
                <div class="layui-input-block">
                    <input id="responsibleName" name="responsibleName" placeholder="请输入负责人"
                           class="layui-input" lay-verify="required" autocomplete="off">
                </div>
            </div>
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">联系电话：</label>
                <div class="layui-input-block">
                    <input id="supplyPhone" name="supplyPhone" placeholder="请输入联系电话"
                           class="layui-input" lay-verify="required" autocomplete="off">
                </div>
            </div>
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱：</label>
                <div class="layui-input-block">
                    <input id="email" name="email" placeholder="请输入邮箱"
                           class="layui-input" lay-verify="required" autocomplete="off">
                </div>
            </div>
        </div>
    </div>
    <div style="display: flex">
        <div class="layui-form-item" style="display: contents">
            <label class="layui-form-label">营业执照：</label>
            <div class="layui-upload-list" id="test2">
                <img class="layui-upload-img" id="demo2" style="width: 190px;height: 190px">
                <input type="hidden" name="license" id="license">
            </div>
        </div>
        <div style="width: 73%;" id="bioarea-wrap">
            <div class="layui-form-item">
                <label class="layui-form-label">省：</label>
                <div class="layui-input-block">
                    <select style="width: 100px;" class="level-1-select" lay-filter="#bioarea-wrap1level"
                            name="province">
                        <option value="">请选择省</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">市：</label>
                <div class="layui-input-block">
                    <select class="level-2-select" lay-filter="#bioarea-wrap2level" name="city">
                        <option value="">请选择市</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">区：</label>
                <div class="layui-input-block">
                    <select class="level-3-select" lay-filter="#bioarea-wrap3level" name="area">
                        <option value="">请选择市</option>
                    </select>
                </div>
            </div>
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">详细地址：</label>
                <div class="layui-input-block">
                    <input id="address" name="address" placeholder="请输入详细地址"
                           class="layui-input" lay-verify="required" autocomplete="off">
                </div>
            </div>
        </div>
    </div>

    <!--  -->
    <!--  -->
    <div class="layui-form-item">
        <label class="layui-form-label">售卖分类：</label>
        <div class="layui-input-block">
            <div id="categoryIds" class="xm-select-demo" style="width: 891px"></div>
        </div>
    </div>
    <!--  -->
    <div class="layui-form-item">
        <label class="layui-form-label">余额：</label>
        <div class="layui-input-block">
            <input id="supplyPrice" name="supplyPrice" placeholder="请输入余额"
                   class="layui-input" lay-verify="required" autocomplete="off">
        </div>
    </div>
    <!--  -->
    <div class="layui-form-item">
        <label class="layui-form-label">介绍：</label>
        <div class="layui-input-block">
            <textarea id="supplyDetail" name="supplyDetail"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">启用状态：</label>
        <div class="layui-input-block">
            <select id="enableState" name="enableState" lay-filter="enableState">
            </select>
        </div>
    </div>
    <div class="layui-form-item" style="text-align: center">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="shop_supply_info-add">立即提交</button>
        </div>
    </div>
</form>
<div th:include="/shop/include::footer"></div>
<script src="../../../layuiadmin/modules/xmSelect.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.config({
        base: '../../../layuiadmin/layui/lay/modules/'
    }).extend({
        bioarea: 'bioarea'
    });
</script>
<script>
    layui.use(['layer', 'jquery', 'form', 'laydate', "bioarea", "layedit", "upload", 'dict'], function () {
        var $ = layui.jquery;
        var laydate = layui.laydate;
        var layer = layui.layer;
        var form = layui.form;
        var bioarea = layui.bioarea;
        var layedit = layui.layedit;
        var upload = layui.upload;
        var dict = layui.dict;

        dict.setSelect("enableState", "enableState");
        ['showFolderIcon', 'showLine', 'strict'].forEach(function (key) {
            layui.form.on('checkbox(' + key + ')', function (data) {
                var treeConfig = {};
                treeConfig[key] = data.elem.checked;
                categoryIds.update({
                    tree: treeConfig
                })
            });
        })


        var categoryIds = xmSelect.render({
            el: '#categoryIds',
            autoRow: true,
            filterable: true,
            tree: {
                show: true,
                showFolderIcon: true,
                showLine: true,
                indent: 20,
                expandedKeys: [-3],
                strict: true
            },
            toolbar: {
                show: true,
                list: ['ALL', 'REVERSE', 'CLEAR']
            },
            autoRow: true,
            filterable: true,
            height: 'auto',
            data: []
        })

        $.ajax({
            url: '/platform/goodscategory/selectAllCategory',
            type: "get",
            success: function (result) {
                var res = result.data;
                var str = "[";
                for (var i = 0; i < res.length; i++) {
                    str += "{name: '" + res[i].categoryName + "', value: " + res[i].id + ", children:[";
                    for (var l = 0; l < res[i].list.length; l++) {
                        str += "{name: '" + res[i].list[l].categoryName + "', value: " + res[i].list[l].id + ", children:[";
                        for (var z = 0; z < res[i].list[l].list.length; z++) {
                            str += "{name: '" + res[i].list[l].list[z].categoryName + "', value: " + res[i].list[l].list[z].id + ", children:[]},";
                        }
                        str += "]},"
                    }
                    str += "]},"
                }
                str += "]";
                categoryIds.update({
                    data: eval("(" + str + ")")
                })
            },
            error: function (result) {
                layer.msg("查询分类失败")
            }
        });


        var supplyDetail = layedit.build('supplyDetail', {
            tool: [
                'strong' //加粗
                , 'italic' //斜体
                , 'underline' //下划线
                , 'del' //删除线

                , '|' //分割线

                , 'left' //左对齐
                , 'center' //居中对齐
                , 'right' //右对齐
                , 'link' //超链接
                , 'unlink' //清除链接
                , 'face' //表情
            ]
        }); //建立编辑器

        var uploadInst1 = upload.render({
            elem: '#test1'
            , url: '/platform/UploadFile' //改成您自己的上传接口
            //, headers: {"token": layui.data(layui.setter.tableName)['token']}
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.status != "SUCCESS") {
                    return layer.msg('上传失败');
                }
                //上传成功
                layer.msg('上传成功');
                $("#supplyHead").val(res.data)
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst1.upload();
                });
            }
        });

        var uploadInst2 = upload.render({
            elem: '#test2'
            , url: '/platform/UploadFile' //改成您自己的上传接口
            //, headers: {"token": layui.data(layui.setter.tableName)['token']}
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo2').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.status != "SUCCESS") {
                    return layer.msg('上传失败');
                }
                //上传成功
                layer.msg('上传成功');
                $("#license").val(res.data)
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst2.upload();
                });
            }
        });

        bioarea.render({
            elem: '#bioarea-wrap',
            defaultData: {
                provinceCode: '110000',
                cityCode: '110100',
                countyCode: '110113'
            },
            form
        })

        //监听提交
        form.on('submit(shop_supply_info-add)', function (data) {
            var selectArr = categoryIds.getValue('valueStr');
            selectArr = JSON.stringify(selectArr, null, 2);
            selectArr = selectArr.replace(/"/g, "");
            data.field.categoryIds = selectArr
            data.field.supplyDetail = layedit.getContent(supplyDetail);
            $.ajax({
                url: '/platform/supplyinfo/add',
                type: "post",
                data: JSON.stringify(data.field),
                contentType: 'application/json; charset=utf-8', // 很重要
                async: false,
                success: function (result) {
                    if (result.status == "SUCCESS") {
                        layer.msg(result.message, {
                            offset: '15px'
                            , icon: 1
                            , time: 1000
                        }, function () {
                            window.parent.location.reload();
                        });
                    } else {
                        layer.msg(result.message)
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>